package com.chw.admin.role;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.chw.annotation.PermissionSecurity;
import com.chw.common.ServiceData;
import com.chw.utils.LogUtil;

@Controller
@RequestMapping("/sysRole")
public class SysRoleController {
	private static LogUtil log = LogUtil.newInstance(SysRoleController.class);

	@Autowired
	private ISysRoleService sysRoleService;

	@RequestMapping(value = "/pageSysRole", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="查询",module="角色管理",btnfunc="select_role")
	public ServiceData page(HttpServletRequest request, SysRole role, Integer currentPage, Integer pageSize)
			throws Exception {
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		PageInfo<SysRole> users = sysRoleService.page(role, currentPage, pageSize);
	    ret.setCode(request, ServiceData.RetCode.Success);
	    ret.setBo(users);
		return ret;
	}
	
	@RequestMapping(value = "/get", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="查询",module="角色管理",btnfunc="select_role")
	public ServiceData get(HttpServletRequest request, String id)
			throws Exception {
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		SysRole role = sysRoleService.selectById(id);
	    ret.setCode(request, ServiceData.RetCode.Success);
	    ret.setBo(role);
		return ret;
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="新增",module="角色管理",btnfunc="add_role")
	public ServiceData save(HttpServletRequest request,SysRole role) throws Exception {
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		sysRoleService.saveOrUpdate(role);
	    ret.setCode(request, ServiceData.RetCode.Success);
		return ret;
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="修改",module="角色管理",btnfunc="update_role")
	public ServiceData update(HttpServletRequest request,SysRole role) throws Exception {
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		sysRoleService.saveOrUpdate(role);
	    ret.setCode(request, ServiceData.RetCode.Success);
		return ret;
	}
	
	@RequestMapping(value = "/updateStatus", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="启用禁用",module="角色管理",btnfunc="update_role_status")
	public void updateStatus(HttpServletRequest request, String id, String status) throws Exception {
		sysRoleService.updateStatus(id, status);
	}
	
	@RequestMapping(value="/delete", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="删除",module="角色管理",btnfunc="delete_role")
	public void delete(HttpServletRequest request, String id) throws Exception{
		log.info("delete");
		sysRoleService.delete(id);
    }
	
	
	@RequestMapping(value="/addGroupToSysRole", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="新增",module="角色用户组管理",btnfunc="")
	public void addGroupToSysRole(HttpServletRequest request,SysRoleGroup roleGroup) throws Exception{
		sysRoleService.addGroupToSysRole(roleGroup);
	}
	
	@RequestMapping(value="/addUserToSysRole", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="新增",module="角色用户管理",btnfunc="")
	public void addUserToSysRole(HttpServletRequest request,SysRoleUser roleUser) throws Exception{
		sysRoleService.addUserToSysRole(roleUser);
	}
	
	@RequestMapping(value="/getUserByRole", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="查询",module="角色用户管理",btnfunc="")
	public ServiceData getUserByRole(HttpServletRequest request,SysRoleUser roleUser) throws Exception{
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		List<Map<String,Object>> list = sysRoleService.getUserByRole(roleUser);
	    ret.setCode(request, ServiceData.RetCode.Success);
	    ret.setBo(list);
		return ret;
    }
	
	@RequestMapping(value="/getGroupByRole", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="查询",module="角色用户组管理",btnfunc="")
	public ServiceData getGroupByRole(HttpServletRequest request,SysRoleGroup roleGroup) throws Exception{
		log.info("getGroupByRole");
		
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		List<Map<String,Object>> list = sysRoleService.getGroupByRole(roleGroup);
	    ret.setCode(request, ServiceData.RetCode.Success);
	    ret.setBo(list);
		return ret;
    }
	
	@RequestMapping(value="/deleteUserFormRole", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="删除",module="角色用户管理",btnfunc="")
	public void deleteUserFormRole(HttpServletRequest request,SysRoleUser roleUser) throws Exception{
		log.info("deleteUserFormGroup");
		sysRoleService.deleteUserFormRole(roleUser);
    }
	
	@RequestMapping(value="/deleteGroupFormRole", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="删除",module="角色用户组管理",btnfunc="")
	public void deleteGroupFormRole(HttpServletRequest request,SysRoleGroup roleGroup) throws Exception{
		log.info("deleteUserFormGroup");
		sysRoleService.deleteGroupFormRole(roleGroup);
    }
	
	@RequestMapping(value = "/hasNameSysRole", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="查询",module="角色管理",btnfunc="select_role")
	public ServiceData hasRoleName(HttpServletRequest request, String name,String id)
			throws Exception {
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		boolean flag = sysRoleService.hasRoleName(name,id);
	    ret.setCode(request, ServiceData.RetCode.Success);
	    ret.setBo(flag);
		return ret;
	}
}
